home *** CD-ROM | disk | FTP | other *** search
- Path: news.nstn.ca!news
- From: keichele@ac.dal.ca (Klaus Eichele)
- Newsgroups: comp.lang.c
- Subject: Re: looping & sscanf
- Date: Sun, 28 Jan 1996 00:57:39 GMT
- Organization: Dalhousie University
- Message-ID: <4ee3lb$q3i@news.nstn.ca>
- References: <DLtqMw.8Ao@emr1.emr.ca>
- NNTP-Posting-Host: rewasylishen.chem.dal.ca
- X-Newsreader: Forte Free Agent 1.0.82
-
- jagrant@emr1.emr.ca (John Grant) wrote:
-
- >In FORTRAN, I can read a set of numbers from a character string with an
- >'internal read':
- > character*100 buffer
- > real x(10)
- > read(buffer,*) (x(i),i=1,n)
-
- >How can I do this easily in C? If I was reading directly from a file, I
- >would use:
- > for(i=0;i<n;i++){
- > fscanf(f,"%g",&x[i]);
- > }
-
- >However, I want to read the values from a string, i.e. using atof() or
- >sscanf() or ???.
-
- >I was hoping to do something like:
- > char buffer[100];
- > char *b=buffer;
- > for(i=0;i<n;i++){
- > sscanf(b,"%g",&x[i]);
- > b+=???;
- > }
- >but I can't figure out how much to advance the pointer.
-
- >Perhaps I should loop through the string using strtok(), converting each
- >substring using atof()? It seems a bit messy, well at least not as
- >straightforward and simple as FORTRAN.
-
- >I'm sure it's straightforward, but I just can't see it (or find it in the FAQ).
- >--
- >John A. Grant jagrant@emr1.emr.ca
- >Airborne Geophysics
- >Geological Survey of Canada, Ottawa
-
- Hi John,
- before starting, I should warn you that I don't have any of the C
- "bibles" around nor have any experience with the following function (I
- assume that someone on this list shall set me straight if there is a
- problem). However, from the books I DO have around it seems like
- strtod() (in stdlib.h) could be the function you are looking for:
-
- double strtod(const char *s, char **endp)
-
- converts the beginning of string s to type double. If endp is not
- NULL, the address of the first character that follows the converted
- substring is assigned to *endp. In case os an error, errno is given a
- nonzero value.
-
- Good Luck,
- Klaus
-
-
- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
- | Klaus Eichele |
- | Department of Chemistry, Dalhousie University |
- | Halifax, N. S., Canada B3H 4J3 |
- | e-mail: keichele@ac.dal.ca |
- | http://ac.dal.ca/~keichele/keichele.html |
- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-
-